package com.lhkj.ct.admin.modules.gauge.scale.controller;


import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.annntation.PreventDuplicateSubmit;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.Validate;
import com.lhkj.ct.meta.modules.gauge.common.enums.GaugeType;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblGaugeQuestion;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblScaleDimension;
import com.lhkj.ct.meta.modules.gauge.model.vo.ScaleDimensionVo;
import com.lhkj.ct.meta.modules.gauge.service.DimensionService;
import com.lhkj.ct.meta.modules.gauge.service.GaugeQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *      量表维度
 * </p>
 *
 * @author jobob
 * @since 2023-10-16
 */
@RestController
@RequestMapping("api/plat/gauge/scale/dim")
@Api(tags = {"6.1.3维度设置"})
@Validated
public class ScaleDimensionController {

    @Resource
    private DimensionService dimensionService;

    @Resource
    private GaugeQuestionService questionService;

    @GetMapping(value = "list")
    @ApiOperation(value = "查询量表所有维度")
    //@RequiresPermissions(value = {"system:scale:get", "system:scale:update"}, logical = Logical.OR)
    public ActionResult<List<ScaleDimensionVo>> list(String scaleId) {
        return ActionResult.success(dimensionService.selectAllByScale(scaleId));
    }

    @GetMapping(value = "questions")
    @ApiOperation(value = "查询量表题目", notes = "题号，题目内容信息")
    //@RequiresPermissions(value = {"system:scale:get", "system:scale:update"}, logical = Logical.OR)
    public ActionResult<List<TblGaugeQuestion>> questions(String scaleId) {
        return ActionResult.success(questionService.selectQutByGaugeId(scaleId, GaugeType.SCALE));
    }

    @GetMapping(value = "{dimId}/get")
    @ApiOperation(value = "维度详情")
    //@RequiresPermissions(value = {"system:scale:get", "system:scale:update"}, logical = Logical.OR)
    public ActionResult<TblScaleDimension> getForm(@PathVariable String dimId) {
        return ActionResult.success(dimensionService.getForm(dimId));
    }

    @PreventDuplicateSubmit
    @PostMapping(value = "insert")
    @ApiOperation(value = "新增维度")
    @Log(title = "量表维度配置", actionType = ActionType.INSERT)
    //@RequiresPermissions(value = {"system:scale:insert", "system:scale:update"}, logical = Logical.OR)
    public ActionResult<Void> insert(@Valid @RequestBody TblScaleDimension dimension) {
        // 校验是否有重复
        Validate.isTrue(dimensionService.checkNameUnique(dimension), "维度名称+性别+年龄区间组合存在重复情况！");
        return ActionResult.judge(dimensionService.save(dimension));
    }

    @PreventDuplicateSubmit
    @PutMapping(value = "update")
    @ApiOperation(value = "编辑维度")
    @Log(title = "量表维度配置", actionType = ActionType.UPDATE)
    //@RequiresPermissions(value = {"system:scale:insert", "system:scale:update"}, logical = Logical.OR)
    public ActionResult<Void> update(@Valid @RequestBody TblScaleDimension dimension) {
        // 校验是否有重复
        Validate.isTrue(dimensionService.checkNameUnique(dimension), "维度名称+性别+年龄区间组合存在重复情况！");
        return ActionResult.judge(dimensionService.update(dimension));
    }

    @Validated
    @DeleteMapping("{dimId}/delete")
    @ApiOperation(value = "删除维度", notes = "支持单个删除")
    @Log(title = "量表维度配置", actionType = ActionType.DELETE)
    //@RequiresPermissions(value = {"system:scale:insert", "system:scale:update"}, logical = Logical.OR)
    public ActionResult<Void> delete(@PathVariable String dimId) {
        if (dimensionService.removeById(dimId) > 0) {
            return ActionResult.success();
        }
        return ActionResult.fail("删除失败");
    }
}
